package com.itheima.imclient100.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.itheima.imclient100.R;
import com.itheima.imclient100.presenter.LogoutPresenter;
import com.itheima.imclient100.presenter.impl.LogoutPresenterImpl;
import com.itheima.imclient100.utils.SPUtils;
import com.itheima.imclient100.utils.ToastUtils;

/**
 * Created by fullcircle on 2017/7/2.
 */

public class PluginFragment extends BaseFragment implements PluginView{
    private LogoutPresenter presenter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_plugin,null);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        //这个方法在onCreateView 之后会立刻调用 传入的第一个参数就是 onCreateView的返回值
        //可以在onCreateView 当中只处理 xml布局转化成view对象的操作
        //在这个onViewCreated 方法当中 通过一个参数 去找到view中要操作的子控件
        presenter = new LogoutPresenterImpl(this);
      Button btn_logout = (Button) view.findViewById(R.id.btn_logout);
        String username = SPUtils.getUsername(getContext());
        btn_logout.setText("退("+username+")出");
        btn_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.logout();
            }
        });
    }

    @Override
    public void onGetLogoutResult(boolean isLogout, String errMsg) {
        if(isLogout){
            startActivity(new Intent(getContext(),LoginActivity.class));
            getActivity().finish();
        }else{
            ToastUtils.showToast(getContext(),"退出失败:"+errMsg);
        }
    }
}
